package com.dzm.demo.myshop.web.admin.abstracts;

import com.dzm.demo.myshop.commons.dto.PageInfo;
import com.dzm.demo.myshop.commons.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractsBaseServiceImpl<T extends BaseEntity, D extends BaseDao<T>> implements BaseService<T> {

    @Autowired
    protected D dao;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> selectAll() {
        return dao.selectAll();
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        dao.delete(id);
    }

    /**
     * 更新
     * @param t
     */
    @Override
    public void update(T t) {
        dao.update(t);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public T getById(Long id) {
        T t = dao.getById(id);
        return t;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteMulti(String[] ids) {
        dao.deleteMulti(ids);
    }

    /**
     * 查询总数
     * @param t
     * @return
     */
    @Override
    public int count(T t) {
        return dao.count(t);
    }

    @Override
    public PageInfo<T> page(int start, int length, int draw, T t) {
        int count  = count(t);

        Map<String,Object> params = new HashMap<>();
        params.put("start",start);
        params.put("length",length);
        params.put("pageParams",t);

        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setDraw(draw);
        pageInfo.setRecordsTotal(count);
        pageInfo.setRecordsFiltered(count);
        pageInfo.setData(dao.page(params));

        return pageInfo;
    }

}
